package com.cloud.lawcase.api.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.cloud.lawcase.api.entity.TcLawRelCauseExcuteType;
import com.cloud.lawcase.common.driver.SimpleInlangDriver;
import com.cloud.lawcase.common.utils.BaseMapperHelper;

public interface TcLawRelCauseExcuteTypeMapper extends BaseMapperHelper<TcLawRelCauseExcuteType> {

	@Select("select t1.law_id AS lawId, t1.excute_type_id AS excuteTypeId, t2.excute_type_name AS excuteTypeName, count(*) total from tc_dic_law_cause t1 left join tc_dic_law_excute_type t2 on t1.excute_type_id = t2.excute_type_id where t1.law_id in (#{ids}) group by t1.law_id ,t1.excute_type_id")
	@Lang(SimpleInlangDriver.class)
	@ResultType(Map.class)
	List<Map<String, Object>> statCauseByLawIds(@Param("ids") Long[] ids);

	@Delete("delete from tc_law_rel_cause_excute_type where law_id in (select law_id from tc_dic_law where group_id = #{groupId})")
	void deleteByGroupId(@Param("groupId") Long groupId);

	@Select("select t1.punish_id, t1.situation_desc, t1.cause_id, t1.excute_type_id, t2.excute_type_name, t2.senior_id from tc_law_rel_cause_excute_type t1 left join tc_dic_law_excute_type t2 on t1.excute_type_id = t2.excute_type_id where t1.law_id = #{lawId}")
	@Results({ 
			@Result(column = "punish_id", property = "punishId", javaType = Long.class),
			@Result(column = "situation_desc", property = "situationDesc", javaType = String.class),
			@Result(column = "cause_id", property = "causeId", javaType = Long.class),
			@Result(column = "excute_type_id", property = "excuteTypeId", javaType = Integer.class),
			@Result(column = "excute_type_name", property = "excuteTypeName", javaType = String.class),
			@Result(column = "senior_id", property = "seniorId", javaType = int.class) })
	List<TcLawRelCauseExcuteType> listExceuteTypeByLawId(@Param("lawId") long lawId);

	
	@Delete("delete from tc_law_rel_cause_excute_type where excute_type_id in (select excute_type_id from tc_dic_law_excute_type where senior_id > 0) and cause_id = #{causeId}")
	void deleteByCauseId(@Param("causeId")long causeId);

	@Select("select t1.punish_id, t1.cause_id, t1.excute_type_id, t2.excute_type_name from tc_law_rel_cause_excute_type t1, tc_dic_law_excute_type t2 where t1.excute_type_id = t2.excute_type_id and t1.cause_id in (#{ids}) GROUP BY t1.punish_id,t1.cause_id")
	@Lang(SimpleInlangDriver.class)
	@Results({ 
		@Result(column = "punish_id", property = "punishId", javaType = Long.class),
		@Result(column = "cause_id", property = "causeId", javaType = Long.class),
		@Result(column = "excute_type_id", property = "excuteTypeId", javaType = Integer.class),
		@Result(column = "excute_type_name", property = "excuteTypeName", javaType = String.class)})
	List<TcLawRelCauseExcuteType> listExceuteTypeByCauseIds(@Param("ids")List<Long> causeIds);

	@Select("select t1.punish_id, t1.cause_id, t1.excute_type_id, t2.excute_type_name from tc_law_rel_cause_excute_type t1, tc_dic_law_excute_type t2 where t1.excute_type_id = t2.excute_type_id and t1.cause_id in (select cause_id from tc_dic_law_cause where cause_code in (#{codes})) GROUP BY t1.excute_type_id,t1.cause_id")
	@Lang(SimpleInlangDriver.class)
	@Results({
			@Result(column = "punish_id", property = "punishId", javaType = Long.class),
			@Result(column = "cause_id", property = "causeId", javaType = Long.class),
			@Result(column = "excute_type_id", property = "excuteTypeId", javaType = Integer.class),
			@Result(column = "excute_type_name", property = "excuteTypeName", javaType = String.class)})
	List<TcLawRelCauseExcuteType> listExceuteTypeByCauseCode(@Param("codes")List<String> causeCodes);
}